package jp.sourceforge.akjrcp.text_douga.effect;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.VideoSystem;

public class FillImageEffect extends VideoEffect{
	protected BufferedImage image;
	protected double fullPaintTime=0.1;//for change image
	
	public FillImageEffect(){
		
	}
	public String toString(){
		String out="FillImageEffect:image="+image+" "+super.toString();
			return out;
	}
	public FillImageEffect(BufferedImage image){
		this.image=image;
	}
	
	public float getFrameTransparent(double t,double maxtime) {
		double time=Math.abs(t);
		
		if(time<fullPaintTime){
			//return 1;//draw full //this is bug.
			return 0.97f;
		}
		
		return 1-(float)(time/(maxtime));//dont minus fullparttime
	}
	
	@Override
	public void effectAfter(DougaPreference preference, BufferedImage baseImage, int frame,double time) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		if(image==null){
			log.warn("img is null");
			return;
		}
		log.warn("after:"+frame);
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getFrameTransparent(time, getAfterTime())));
		g2.drawImage(image, 0, 0, baseImage.getWidth(), baseImage.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
	}

	@Override
	public void effectAll(DougaPreference preference, BufferedImage baseImage, int frame) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		if(image==null){
			log.warn("img is null");
			return;
		}
		g2.drawImage(image, 0, 0, baseImage.getWidth(), baseImage.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
	}

	@Override
	public void effectBefore(DougaPreference preference, BufferedImage baseImage, int frame,double time) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		if(image==null){
			log.warn("img is null");
			return;
		}
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getFrameTransparent(time, getBeforeTime())));
		log.warn("before:"+frame);
		g2.drawImage(image, 0, 0, baseImage.getWidth(), baseImage.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
	}

	public double getFullPaintTime() {
		return fullPaintTime;
	}

	public void setFullPaintTime(double fullPaintTime) {
		this.fullPaintTime = fullPaintTime;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	@Override
	public void effectKeyFrame(DougaPreference preference, BufferedImage baseImage, int frame, double time) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		if(image==null){
			log.warn("img is null");
			return;
		}
		
		g2.drawImage(image, 0, 0, baseImage.getWidth(), baseImage.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
	}

}
